package com.web.exiaodao.remote.controller;

import com.web.exiaodao.business.entity.Category;
import com.web.exiaodao.remote.service.*;
import com.web.exiaodao.remote.vo.*;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.XaUtil;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author duandazhi
 * @ClassName: ApiSearchController
 * @Description: 搜索接口
 * @date 2016/12/9 下午4:45
 */
@Api(value = "home", description = "首页接口", position = 10)
@Controller
@RequestMapping("/api/home")
public class ApiHomeController extends BaseController {
    private static final Logger LOGGER = Logger.getLogger(ApiHomeController.class);

    @Autowired
    private ApiRollTitleTableService rollTitleTableService;//首页滚动文字
    @Autowired
    private ApiHomeService homeService;//首页搜索；老系统数据导入
    @Autowired
    private ApiHomeSkinService homeSkinService;//首页换肤
    @Autowired
    private ApiSysAreaService sysAreaService;//城市列表

    /**
     * @return
     * @Title:
     * @Description: 全局信息搜索, 只搜索名称
     */
    @ApiOperation(value = "全局信息搜索,只搜索名称", notes = "全局信息搜索,只搜索名称,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "search", method = RequestMethod.POST)
    public XaResult<List<SearchVo>> homeSearch(
            @ApiParam("{\"searchKey\":\"加湿器\"}") @RequestBody(required = false) SearchVo.ApiSearch param
    ) throws BusinessException {
        XaResult<List<SearchVo>> xr = new XaResult<>();
        if (XaUtil.isEmpty(param) || XaUtil.isEmpty(param.getSearchKey().trim())) {
            xr.error("searchKey不能为空", true);
            return xr;
        }
        try {
            xr = homeService.searchByKey(param.getSearchKey());
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage(), true);
            } else {
                xr.error(XaConstant.Message.error, true);
            }
            LOGGER.error(XaConstant.Message.error, new Throwable(e));
        }
        return xr;
    }


    /**
     * @return
     * @Title: findRollTitleTableList
     * @Description: 查询所有滚动栏表信息
     */
    @ApiOperation(value = "查询所有滚动栏表", notes = "查询所有滚动栏表信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findRollTitleList", method = RequestMethod.POST)
    public XaResult<List<RollTitleTableVo>> findRollTitleTableList(
    ) throws BusinessException {
        Pageable pageable = WebUitl.buildPageRequest(0, Integer.MAX_VALUE, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        return rollTitleTableService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
    }

    @Autowired
    private ApiShopCategoryService shopCategoryService;

    /**
     * @return
     * @Title: findShopCategoryList
     * @Description: 查询所有商品|咨询分类
     */
    @ApiOperation(value = "查询所有商品|咨询分类", notes = "查询所有商品|咨询分类,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findShopCategoryList", method = RequestMethod.POST)
    public XaResult<List<CategoryVo>> findShopCategoryList(
            @ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
            @ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("分类,1：商品 ：2：资讯") @RequestParam(defaultValue = Category.DEFAULT_TYPE + "", required = false) Integer type
    ) throws BusinessException {
        XaResult<List<CategoryVo>> xr = new XaResult<List<CategoryVo>>();
        Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'categoryOrder',direction:'ASC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        if (XaUtil.isNotEmpty(type)) {
            filterParams = WebUitl.getParametersStartingWith("{'search_EQ_type':'" + type + "'}", "search_");
        }
        return shopCategoryService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
    }

    /**
     * @return
     * @Title: findSysAreaList
     * @Description: 查询所有城市区域信息
     */
    @ApiOperation(value = "查询所有城市区域", notes = "查询所有城市区域信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findSysAreaList", method = RequestMethod.POST)
    public XaResult<List<SysAreaVo>> findSysAreaList(
    ) throws Exception {
        XaResult<List<SysAreaVo>> xr = new XaResult<List<SysAreaVo>>();
        Pageable pageable = WebUitl.buildPageRequest(0, Integer.MAX_VALUE, "[{property:'orderNum',direction:'ASC'},{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        return sysAreaService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
    }


    /**
     * @return
     * @Title: Franchise agreement list
     * @Description: 加盟协议列表
     */
    @ApiOperation(value = "加盟协议列表", notes = "查询所有加盟协议列表,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findFranchiseAgreementList", method = RequestMethod.POST)
    public XaResult<List<FranchiseAgreementVo>> findFranchiseAgreementList(
    ) throws Exception {
        XaResult<List<FranchiseAgreementVo>> xr = new XaResult<>();
        xr.setObject(FranchiseAgreementVo.getAllAgreements());
        return xr;
    }


    /**
     * @return
     * @Title: findHomeSkinList
     * @Description: 查询所有首页换肤信息
     */
    @ApiOperation(value = "查询所有首页换肤", notes = "查询所有首页换肤信息,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "findHomeSkinList", method = RequestMethod.POST)
    public XaResult<List<HomeSkinVo>> findHomeSkinList(
    ) throws Exception {
        XaResult<List<HomeSkinVo>> xr = new XaResult<List<HomeSkinVo>>();
        Pageable pageable = WebUitl.buildPageRequest(0, Integer.MAX_VALUE, "[{property:'createTime',direction:'DESC'}]");
        Map<String, Object> filterParams = WebUitl.getParametersStartingWith("{}", "search_");
        xr = homeSkinService.findListEQStatusByFilter(XaConstant.Status.valid, filterParams, pageable);
        return xr;
    }

    /**
     * @return
     * @Title: importCustomersUserData
     * @Description: 导入客户系统的数据
     */
    @ApiOperation(value = "导入客户系统的数据", notes = "导入客户系统的数据,当返回的code=1时，取出object进行显示,存放为数组对象")
    @ResponseBody
    @RequestMapping(value = "importCustomersUserData", method = RequestMethod.POST)
    public XaResult<List<UserVo>> importCustomersUserData(
            @ApiParam("导入的客户的excel表格,字段名:excelFile,必填") @RequestParam(value = "excelFile") MultipartFile excelFile
    ) throws Exception {
        XaResult<List<UserVo>> xr = new XaResult<>();
        homeService.importCustomersUserData(excelFile.getInputStream());
        return xr;
    }
}
